package com.chaodriod.common.fragment;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by CHEN on 2017/12/5.
 */

public abstract class ViewPagerFragment extends BaseFragment {

    public ViewPager mViewPager;
    public ViewPagerAdapter mAdapter;

    @Override
    @CallSuper
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mViewPager = (ViewPager) findViewById(getViewPager());
        mAdapter = new ViewPagerAdapter(getManager());
        mViewPager.setAdapter(mAdapter);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter{

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return getFragment(position);
        }

        @Override
        public int getCount() {
            return getFragmentCount();
        }
    }

    @NonNull @IdRes
    public abstract int getViewPager();

    @NonNull
    public abstract FragmentManager getManager();

    @NonNull
    public abstract Fragment getFragment(int position);

    public abstract int getFragmentCount();
}
